import globals from "globals";
import pluginJs from "@eslint/js";
import pluginTypescript from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";
import pluginPrettier from "eslint-plugin-prettier";
import parserVue from "vue-eslint-parser";
import prettierConfig from "./prettier.config.mjs";
import autoImport from "./auto-import.cjs";

export default [
    ...pluginTypescript.configs.recommended,
    {
        // eslint 默认推荐规则
        ...pluginJs.configs.recommended,
        languageOptions: {
            globals: {
                ...globals.browser,
                ...globals.es2020,
                ...globals.node,
                ...autoImport.globals, // 自动导入的变量
            },
            ecmaVersion: 2020,
        },
        rules: {
            camelcase: 0,
            // 要求使用 let 或 const 而不是 var
            "no-var": 2,
            "no-undef": 0,
            "no-unused-vars": [0, { caughtErrors: "none" }], // 未使用变量
            "no-console": process.env.NODE_ENV === "production" ? 2 : 0,
            "no-debugger": process.env.NODE_ENV === "production" ? 2 : 0,
            // 禁止不必要的转义字符
            "no-useless-escape": 0,
        },
    },
    {
        files: ["**/*.?([cm])ts", "**/*.?([cm])tsx"],
        languageOptions: {
            parser: pluginTypescript.parser,
            parserOptions: {
                sourceType: "module",
                parser: pluginTypescript.parser,
                tsconfigRootDir: import.meta.dirname,
                project: ["./tsconfig.json"],
            },
        },
        rules: {
            "@typescript-eslint/no-this-alias": 0,
            "@typescript-eslint/no-explicit-any": [0],
            // 关闭对于 TypeScript 注释的检查
            "@typescript-eslint/ban-ts-comment": 0,
            "@typescript-eslint/no-namespace": 0,
            "@typescript-eslint/no-unused-vars": [0, { caughtErrors: "none" }], // 未使用变量
            // 可以轻松推断的显式类型可能会增加不必要的冗长
            "@typescript-eslint/no-inferrable-types": 0,
        },
    },
    {
        files: ["**/*.?([cm])js"],
        rules: {
            "@typescript-eslint/no-require-imports": "off",
            "@typescript-eslint/no-var-requires": "off",
            "@typescript-eslint/no-unused-vars": [0, { caughtErrors: "none" }], // 未使用变量
        },
    },
    {
        files: ["**/*.d.ts"],
        rules: {
            "eslint-comments/no-unlimited-disable": "off",
            "import/no-duplicates": "off",
            "@typescript-eslint/no-unused-vars": [0, { caughtErrors: "none" }], // 未使用变量
        },
    },
    {
        files: ["**/*.vue"],
        languageOptions: {
            globals: {},
            parser: parserVue,
            parserOptions: {
                ecmaFeatures: {
                    jsx: true,
                },
                extraFileExtensions: [".vue"],
                sourceType: "module",
                parser: pluginTypescript.parser,
            },
        },
        plugins: {
            vue: pluginVue,
        },
        processor: pluginVue.processors[".vue"],
        rules: {
            // vue3 基础推荐规则
            ...pluginVue.configs.base.rules,
            ...pluginVue.configs["vue3-essential"].rules,
            ...pluginVue.configs["vue3-recommended"].rules,
            "vue/require-default-prop": 0,
            // 关闭对于组件 props 需要默认值的检查
            "vue/require-default-prop": 0,
            // 关闭对于单行 HTML 元素内容换行的检查
            "vue/singleline-html-element-content-newline": 0,
            // 关闭对于每行 HTML 属性数量的限制检查
            "vue/max-attributes-per-line": 0,
            // 强制自定义事件的命名为驼峰式命名
            "vue/custom-event-name-casing": [2, "camelCase"],
            // 显示警告，不建议使用 v-text 指令
            "vue/no-v-text": 1,
            // 显示警告，建议在 Vue 组件中添加间距行
            "vue/padding-line-between-blocks": 1,
            // 显示警告，建议直接导出 Vue 组件
            "vue/require-direct-export": 1,
            // 允许使用单个单词作为组件名
            "vue/multi-word-component-names": 0,
            // 关闭对于 TypeScript 注释的检查
            "@typescript-eslint/ban-ts-comment": 0,
            // 关闭对于显式声明 any 类型的检查
            "@typescript-eslint/no-explicit-any": 0,
            "@typescript-eslint/no-unused-vars": [0, { caughtErrors: "none" }], // 未使用变量
            "no-unused-vars": "off",
        },
    },
    {
        plugins: {
            prettier: pluginPrettier,
        },
        rules: {
            // prettier 默认推荐规则
            ...pluginPrettier.configs.recommended.rules,
            "prettier/prettier": [
                "error",
                {
                    ...prettierConfig,
                },
            ],
        },
    },
    {
        ignores: [
            "dist",
            "node_modules",
            "public",
            ".husky",
            ".vscode",
            ".idea",
            "*.sh",
            "*.md",
            "*.woff",
            "*.ttf",
            "src/assets",
            "eslint.config.mjs",
            "prettier.config.mjs",
            ".stylelintrc.cjs",
        ],
    },
];
